﻿Imports Abyx.WindowsExtendedControls
Imports Abyx.SVCMS.Library.DB
Imports Abyx.SVCMS.Library

Public Class STK_InventoryBatches_Form
    Implements ICarfDataEditingChildWindow


    Dim frmMain As SVCM_Form = Nothing
    Dim iDataObject As InventoryBatchTypeCollection
    Dim iSelectedInventoryBatch As InventoryBatchType = Nothing
    Dim currentView As STK_InventoryMethodsEnum = STK_InventoryMethodsEnum.GRN_Entry

#Region "Constructors/Startup methods"
    Private Sub STK_InventoryBatches_Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Refresh_InventoryBatchesData()
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub STK_InventoryBatches_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Public Overloads Sub Show()
        MyBase.Show()
        Me.frmMain = Me.MdiParent
        UpdateFormView()
    End Sub

#End Region

#Region "Events"

    'Search
    Private Sub Search_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Search_ImageButton.Click
        UpdateFormView()
    End Sub
    Private Sub Search_TextBox_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Search_TextBox.KeyUp
        If e.KeyCode = Keys.Enter Then
            Search_ImageButton.PerformClick()
        End If
    End Sub

    'Panel Switching
    Private Sub GRN_VerticalLabel_Click(sender As System.Object, e As System.EventArgs) Handles GRN_VerticalLabel.Click
        Me.currentView = STK_InventoryMethodsEnum.GRN_Entry
        Me.UpdateFormView()
    End Sub
    Private Sub FileLoad_VerticalLabel_Click(sender As System.Object, e As System.EventArgs) Handles FileLoad_VerticalLabel.Click
        Me.currentView = STK_InventoryMethodsEnum.File_Import
        Me.UpdateFormView()
    End Sub

    'Item select / open
    Private Sub Items_DetailListBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Batches_DetailListBox.SelectedIndexChanged
        If Me.Batches_DetailListBox.SelectedItem IsNot Nothing Then
            Dim item As DetailListBoxItem = Me.Batches_DetailListBox.SelectedItem
            Me.iSelectedInventoryBatch = item.DataObject
            Refresh_StockItemsForSelectedBatch()
        End If
    End Sub
    Private Sub Batches_DetailListBox_DoubleClick(sender As Object, e As System.EventArgs) Handles Batches_DetailListBox.DoubleClick
        If Me.Batches_DetailListBox.SelectedItem IsNot Nothing Then
            Dim item As DetailListBoxItem = Me.Batches_DetailListBox.SelectedItem
            If item.DataObject IsNot Nothing Then
                Dim batch As InventoryBatchType = item.DataObject
                If batch.BatchType = STK_InventoryMethodsEnum.GRN_Entry Then
                    Dim frm As STK_Grn_Form = AppSettings.GetFormInstanceForDataObject(batch)
                    If frm Is Nothing Then
                        frm = New STK_Grn_Form
                    End If
                    frm.MdiParent = AppSettings.MainForm
                    frm.Show(batch)
                End If

            End If
        End If
    End Sub

    'Close
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub

#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.InventoryBatch, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.StockItem, _
                RelatedControlDataCategoriesEnum.StockItemInventory, _
                RelatedControlDataCategoriesEnum.InventoryBatch
                AppSettings.MainForm.StatusInfoMsgPut("The data being viewed has been changed elsewhere, " & _
                                                      "and hence was refreshed automatically")
                Refresh_InventoryBatchesData()

        End Select

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject

    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub UpdateFormView()
        If Me.currentView = STK_InventoryMethodsEnum.GRN_Entry Then
            GRN_VerticalLabel.ControlBackColorNormal = Color.DimGray
            FileLoad_VerticalLabel.ControlBackColorNormal = SystemColors.InactiveBorder
            Refresh_GrnsList()
        Else
            GRN_VerticalLabel.ControlBackColorNormal = SystemColors.InactiveBorder
            FileLoad_VerticalLabel.ControlBackColorNormal = Color.DimGray
            Refresh_FileImportsList()
        End If
    End Sub
    Private Sub Refresh_InventoryBatchesData()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Me.iDataObject = stkMan.Get_InventoryBatches_All_Col
        Refresh_GrnsList()
        Refresh_FileImportsList()
    End Sub
    Private Sub Refresh_GrnsList()
        Dim grnList = From sGrn As InventoryBatchType In Me.iDataObject _
                      Where sGrn.BatchType = STK_InventoryMethodsEnum.GRN_Entry _
                      Order By sGrn.GrnReferenceNo
        Me.Batches_DetailListBox.Items.Clear()
        If (grnList IsNot Nothing) _
                    AndAlso (grnList.Count > 0) Then
            For i As Integer = 0 To grnList.Count - 1
                Dim grn As InventoryBatchType = grnList(i)
                Dim d As DetailListBoxItem = grn.ToDetailsListBoxItem
                If (d.Header & " " & d.Details & " " & d.Footer).ToLower.Contains(Search_TextBox.Text.ToLower) Then
                    Me.Batches_DetailListBox.Items.Add(d)
                End If
            Next
            If Me.Batches_DetailListBox.Items.Count > 0 Then
                Me.Batches_DetailListBox.SelectedIndex = 0
            Else
                Me.iSelectedInventoryBatch = Nothing
                Refresh_StockItemsForSelectedBatch()
            End If
        End If
    End Sub
    Private Sub Refresh_FileImportsList()
        Dim fileList = From sGrn As InventoryBatchType In Me.iDataObject _
                      Where sGrn.BatchType = STK_InventoryMethodsEnum.File_Import _
                      Order By sGrn.GrnReferenceNo
        Me.Batches_DetailListBox.Items.Clear()
        If (fileList IsNot Nothing) _
                    AndAlso (fileList.Count > 0) Then
            For i As Integer = 0 To fileList.Count - 1
                Dim file As InventoryBatchType = fileList(i)
                Dim d As DetailListBoxItem = file.ToDetailsListBoxItem
                If (d.Header & " " & d.Details & " " & d.Footer).ToLower.Contains(Search_TextBox.Text.ToLower) Then
                    Me.Batches_DetailListBox.Items.Add(d)
                End If
            Next
            If Me.Batches_DetailListBox.Items.Count > 0 Then
                Me.Batches_DetailListBox.SelectedIndex = 0
            Else
                Me.iSelectedInventoryBatch = Nothing
                Refresh_StockItemsForSelectedBatch()
            End If
        End If
    End Sub
    Private Sub Refresh_StockItemsForSelectedBatch()
        If Me.iSelectedInventoryBatch IsNot Nothing Then
            Dim stkMan As New StkManager(AppSettings.LoggedUser)
            Dim col As InventoryTypeCollection = stkMan.Get_Inventory_ForBactchId_Col(Me.iSelectedInventoryBatch.InventoryBatchId)
            Me.BatchInventory_ExtendedDataGridView.AutoGenerateColumns = False
            BatchInventory_ExtendedDataGridView.DataSource = col
        Else
            BatchInventory_ExtendedDataGridView.DataSource = Nothing
        End If
    End Sub
#End Region


End Class

